package com.lonely.fx.choice_box;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @date: 2022-07-17 22:24
 * @author: ztkj-hzb
 * @description:
 **/
public class 联动效果演示 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();

        ChoiceBox<String> box1 = new ChoiceBox<>();
        ChoiceBox<String> box2 = new ChoiceBox<>();


        ObservableList<String> list1 = FXCollections.observableArrayList("数字", "字母");
        ObservableList<String> list2 = FXCollections.observableArrayList("1", "2", "3", "4", "5", "6");
        ObservableList<String> list3 = FXCollections.observableArrayList("一", "二", "三", "四", "五", "六");

        box1.setItems(list1);

        //box1切换事件
        box1.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {

            if (newValue == null || newValue.equals("")) {
                return;
            }

            if ("数字".equals(newValue)) {
                box2.setItems(list2);
            }

            if ("字母".equals(newValue)) {
                box2.setItems(list3);
            }

            box2.show();
        });


        box1.setPrefWidth(200);
        box2.setPrefWidth(200);
        hBox.setSpacing(100);
        hBox.getChildren().addAll(box1, box2);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();

        //展开box1
        box1.show();
    }
}
